<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<title>地图图片上传</title>
<head th:include="include :: header"></head>
<link th:href="@{/ajax/libs/summernote/summernote.css}" rel="stylesheet"/>
<link th:href="@{/ajax/libs/summernote/summernote-bs3.css}" rel="stylesheet"/>
<body class="white-bg">

<div class="wrapper wrapper-content animated fadeInRight ibox-content" >
    <form class="form-horizontal m">

        <div class="form-group">
            <label class="col-sm-3 control-label">图片：</label>
            <div class="col-sm-8">
                <div id="editor" class="summernote"></div>
            </div>
        </div>
    </form>
</div>

<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/cropbox/cropbox.js}"></script>
<script th:src="@{/ajax/libs/summernote/summernote.min.js}"></script>
<script th:src="@{/ajax/libs/summernote/summernote-zh-CN.js}"></script>
<script type="text/javascript" th:inline="javascript">
    var avatar = [[${lvyou.avatar}]];
    $(function () {
        $('.summernote').summernote({
            height: '500px',
            width: '100%',
            lang: 'zh-CN',
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }, toolbar: [

                ['insert', ['picture']],//自定义按钮展示
            ]
        });
        console.log(avatar)
        $('#editor').summernote('code', avatar);
    });


    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    // $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    $(obj).summernote('editor.insertImage', img_root + result.fileName, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    function submitHandler() {
        var formdata = new FormData();
        formdata.append("avatar", $('.summernote').summernote('code'));
        formdata.append("lvId", [[${lvyou.lvId}]]);
        $.ajax({
            url: ctx + "system/lvyou/updateAvatar",
            data: formdata,
            type: "post",
            processData: false,
            contentType: false,
            success: function (result) {
                $.operate.saveSuccess(result);
            }
        })
    }

</script>
</body>
</html>
